package net.cyndeline.rlgraph.util

import scalax.collection.immutable.{ Graph => ScalaGraph }
import org.jgrapht.{UndirectedGraph, Graph}
import org.jgrapht.graph.{DefaultEdge, SimpleGraph}
import scalax.collection.GraphEdge.UnDiEdge

/**
 * Converts a scala-graph to JGraphT.
 */
class ScalaGraphToJGraphTConverter {

  /**
   * Adds every vertex and edge of a scala-graph to a JGraphT graph.
   *
   * @param graph Scala graph to convert.
   * @return a JGraphT representation of the scala graph.
   */
  def convert[VType, EType[X] <: UnDiEdge[X]](graph: ScalaGraph[VType, EType]): UndirectedGraph[VType, DefaultEdge] = {
    val jGraphTGraph = new SimpleGraph[VType, DefaultEdge](classOf[DefaultEdge])

    val nodes = graph.nodes.iterator
    while (nodes.hasNext) {
      val n = nodes.next()
      jGraphTGraph.addVertex(n)
    }

    val edges = graph.edges.iterator
    while (edges.hasNext) {
      val e = edges.next()
      jGraphTGraph.addEdge(e._1, e._2)
    }

    jGraphTGraph
  }
}
